package com.pansoft.openplanet.fragment;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.activity.AirDropDetailActivity;
import com.pansoft.openplanet.bean.AirDropModel;
import com.pansoft.openplanet.fragment.AirDropFragment.OnAirDropFragmentInteractionListener;

import java.util.List;

/**
 * 空投场列表adapter
 */
public class AirDropRecyclerViewAdapter extends RecyclerView.Adapter<AirDropRecyclerViewAdapter.ViewHolder> {

    private final List<AirDropModel.AirportlistBean> mValues;
    private final OnAirDropFragmentInteractionListener mListener;
    private Context mContext;

    public AirDropRecyclerViewAdapter(Context mContext, List<AirDropModel.AirportlistBean> items, OnAirDropFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.assets_fragment_item_air_drop, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);

        int status = holder.mItem.getStatus();
        if (status == AirDropModel.STATE_STARTED) {
            holder.butGo.setText(ResStringUtil.getString(R.string.air_drop_detail_but_text));
            GradientDrawable gd = (GradientDrawable) holder.butGo.getBackground();
            gd.setColor(mContext.getResources().getColor(R.color.op_text_accent_color));
        } else if (status == AirDropModel.STATE_UNSTART) {
            holder.butGo.setText(ResStringUtil.getString(R.string.open_planet_airdrop_state_not_start));
            GradientDrawable gd = (GradientDrawable) holder.butGo.getBackground();
            gd.setColor(mContext.getResources().getColor(R.color.gray_bg));
        } else if (status == AirDropModel.STATE_STOP) {
            holder.butGo.setText(ResStringUtil.getString(R.string.open_planet_airdrop_state_end));
            GradientDrawable gd = (GradientDrawable) holder.butGo.getBackground();
            gd.setColor(mContext.getResources().getColor(R.color.gray_bg));
        } else {
            holder.butGo.setText(ResStringUtil.getString(R.string.open_planet_airdrop_state_out));
            GradientDrawable gd = (GradientDrawable) holder.butGo.getBackground();
            gd.setColor(mContext.getResources().getColor(R.color.gray_bg));
        }
        if (  holder.mItem.getFlag()==1){
            //已经抢过
            holder.butGo.setText(ResStringUtil.getString(R.string.open_planet_airdrop_state_had));
            GradientDrawable gd = (GradientDrawable) holder.butGo.getBackground();
            gd.setColor(mContext.getResources().getColor(R.color.gray_bg));
        }

        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        if (status == AirDropModel.STATE_UNSTART) {
            spannableStringBuilder.append(ResStringUtil.getString(R.string.open_planet_airdrop_time_before_start));
        } else {
            spannableStringBuilder.append(ResStringUtil.getString(R.string.open_planet_airdrop_time_before_end));

        }

        SpannableString day = new SpannableString(convertToString(holder.mItem.getEndTimeOfDay()));
        ForegroundColorSpan textColor2 = new ForegroundColorSpan(mContext.getResources().getColor(R.color.op_bonus_color));
        day.setSpan(textColor2, 0, day.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(day);
        spannableStringBuilder.append(ResStringUtil.getString(R.string.common_text_time_day));
        SpannableString hour = new SpannableString(convertToString(holder.mItem.getEndTimeOfHour()) + "");
        ForegroundColorSpan textColor = new ForegroundColorSpan(mContext.getResources().getColor(R.color.op_bonus_color));
        hour.setSpan(textColor, 0, hour.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(hour);
        spannableStringBuilder.append(ResStringUtil.getString(R.string.common_text_time_hour));
        SpannableString minute = new SpannableString(convertToString(holder.mItem.getEndTimeOfMinute()) + "");
        ForegroundColorSpan color1 = new ForegroundColorSpan(mContext.getResources().getColor(R.color.op_bonus_color));
        minute.setSpan(color1, 0, minute.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(minute);
        spannableStringBuilder.append(ResStringUtil.getString(R.string.common_text_time_minute));

        holder.tvResidueTime.setText(spannableStringBuilder);
        holder.progressbar.setMax(100);
        int percent = (int) (Double.valueOf(holder.mItem.getDistributionPercent()) * 100);
        holder.progressbar.setProgress(percent);
        holder.tvTeamName.setText(holder.mItem.getOrganization());
        holder.tvTitle.setText(holder.mItem.getBrief());
        holder.tvProgress.setText(mContext.getResources().getString(R.string.open_planet_airdrop_had_percent, percent+""));
        holder.tvAssetName.setText(holder.mItem.getCoinName()+"");


        LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, holder.ivIamge, holder.mItem.getAirportPicture(),
                LXGlideImageLoader.DEFATLT_AVATAR_RADIUS_10);
        holder.butGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AirDropDetailActivity.start(mContext, holder.mItem.getId());
            }
        });
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AirDropDetailActivity.start(mContext, holder.mItem.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        //        public final TextView mIdView;
//        public final TextView mContentView;
        public AirDropModel.AirportlistBean mItem;
        private CardView cardView;
        private TextView tvTeamName;
        private TextView tvResidueTime;
        private ImageView ivIamge;
        private TextView tvTitle;
        private TextView tvProgress;
        private ProgressBar progressbar;
        private Button butGo;
        private TextView tvAssetName;


        public ViewHolder(View view) {
            super(view);
            mView = view;
            cardView = (CardView) view.findViewById(R.id.cardView);
            tvTeamName = (TextView) view.findViewById(R.id.tv_team_name);
            tvResidueTime = (TextView) view.findViewById(R.id.tv_residue_time);
            ivIamge = (ImageView) view.findViewById(R.id.iv_iamge);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvProgress = (TextView) view.findViewById(R.id.tv_progress);
            progressbar = (ProgressBar) view.findViewById(R.id.progressbar);
            butGo = (Button) view.findViewById(R.id.but_go);
            tvAssetName = (TextView) view.findViewById(R.id.tv_asset_name);
        }


    }

    public static String convertToString(int value) {
        String result = String.valueOf(value);
        if (result.length() == 1) {
            result = "0" + result;
        }
        return result;
    }
}
